package part18;

import java.io.*;

/**
 * @author wcj
 * @Date 2021/6/4 18:15
 * @Version 1.0
 */
public class Part1833 {

    public static void main(String[] args) throws Exception {
        Blip1 blip1 = new Blip1();
        Blip2 blip2 = new Blip2();
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Blips.out"));
        out.writeObject(blip1);
        out.writeObject(blip2);
        out.close();

        ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blips.out"));
        Blip1 blip11 = (Blip1) in.readObject();
        Blip2 blip12 = (Blip2) in.readObject();
    }
}

/**
 * Serializable接口，将对象的所有序列化的事情全部隐藏了
 * 如果我们想要对对象的序列化和反序列进行控制，那么我们可以实现Externalizable接口，其中有两个方法，writeExternal和readExternal
 * 分别在序列化和反序列化的过程中执行
 * Serializable和Externalizable的区别为：前者无法对序列化和反序列化过程进行控制，后者可以
 * 前者对于对象的反序列化的恢复是纯粹根据二进制文件恢复的，而后者会调用默认的构造器，因此构造器需要是public的
 */
class Blip1 implements Externalizable {

    public Blip1() {
        System.out.println("Blip1 Constructor!");
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        System.out.println("Blip1 writeExternal");
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        System.out.println("Blip1 readExternal");
    }
}

class Blip2 implements Externalizable {

    public Blip2() {
        System.out.println("Blip2 Constructor");
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        System.out.println("Blip2 writeExternal");
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        System.out.println("Blip2 readExternal");
    }
}
